package net.whzjkj.gateway.tcpserver.rabbit;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;

import java.io.IOException;

/**
 * @Author: shikang
 * @Description: 消息数据处理
 * @Date: Created in 2018/3/26 17:13
 */
@Slf4j
public class MessageRabbitTask implements Runnable {

    private Channel channel;

    private Message message;

    public MessageRabbitTask( Message message, Channel channel) {
        this.message = message;
        this.channel = channel;
    }

    @Override
    public void run() {
        String msg = null;
        try {
            //获取队列数据
            msg = new String(message.getBody(), "UTF-8");
            log.info("消息数据队列上传数据:" + msg);


            //消息回复
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("消息数据获取队列数据出错", e);
            try {
                if(message.getMessageProperties().getRedelivered()) {
                    log.error("消息数据消息(" + msg + ")重复处理失败，拒绝再次接收");
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
                } else {
                    log.error("消息数据消息(" + msg + ")处理失败，返回队列");
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                }
            } catch (IOException e1) {

            }
        }
    }
}
